/**
 *  Copyright [2011] Steffen Kämpke
 *  mailto: steffen.kaempke@pennychecker.de
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pennychecker.example.mvp.view;

import com.pennychecker.example.mvp.model.User;
import com.pennychecker.example.mvp.presenter.AllUserPresenter;
import com.pennychecker.swing.presenter.ext.event.HasClickHandlers;
import com.pennychecker.swing.presenter.ext.event.HasDoubleClickHandlers;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;

/**
 *
 * @author Steffen Kämpke mailto:steffen.kaempke@pennychecker.de
 */
public class AllUserView extends javax.swing.JPanel implements AllUserPresenter.Display {

    private static final long serialVersionUID = 1L;

    /** Creates new form AllUserView */
    public AllUserView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        listUsers = new com.pennychecker.swing.presenter.ext.component.SList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        textLabelFirstname = new javax.swing.JLabel();
        textLabelLastname = new javax.swing.JLabel();
        buttonAddUser = new com.pennychecker.swing.presenter.ext.component.SButton();

        setName("Users"); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        listUsers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listUsers.setName("listUsers"); // NOI18N
        jScrollPane1.setViewportView(listUsers);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 174;
        gridBagConstraints.ipady = 127;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        add(jScrollPane1, gridBagConstraints);

        jLabel1.setText("Firstname:");
        jLabel1.setName("jLabel1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 0);
        add(jLabel1, gridBagConstraints);

        jLabel2.setText("Lastname:");
        jLabel2.setName("jLabel2"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 11, 0, 0);
        add(jLabel2, gridBagConstraints);

        textLabelFirstname.setText("-");
        textLabelFirstname.setName("textLabelFirstname"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 126;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 6, 0, 0);
        add(textLabelFirstname, gridBagConstraints);

        textLabelLastname.setText("-");
        textLabelLastname.setName("textLabelLastname"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 126;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
        add(textLabelLastname, gridBagConstraints);

        buttonAddUser.setText("add user");
        buttonAddUser.setName("buttonAddUser"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(buttonAddUser, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.pennychecker.swing.presenter.ext.component.SButton buttonAddUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private com.pennychecker.swing.presenter.ext.component.SList listUsers;
    private javax.swing.JLabel textLabelFirstname;
    private javax.swing.JLabel textLabelLastname;
    // End of variables declaration//GEN-END:variables

    public HasClickHandlers getUserListClick() {
        return listUsers;
    }

    public HasDoubleClickHandlers getUserListDoubleClick() {
        return listUsers;
    }

    public int getSelectedUserIndex() {
        return listUsers.getSelectedIndex();
    }

    public void setSelectedUser(User selectedUser) {
        textLabelFirstname.setText(selectedUser.getFirstname());
        textLabelLastname.setText(selectedUser.getLastname());
    }

    public void setUsers(List<User> users) {
        final DefaultListModel model = new DefaultListModel();

        for (int i = 0; i < users.size(); i++) {
            final User user = users.get(i);
            model.add(i, user.getFirstname() + " " + user.getLastname());
        }

        listUsers.setModel(model);
    }

    public void clearSelections() {
        listUsers.clearSelection();
    }

    public void setSelection(int index) {
        listUsers.setSelectedIndex(index);
    }

    public JComponent asComponent() {
        return this;
    }

    public HasClickHandlers getAddUserButton() {
        return buttonAddUser;
    }
}
